package fourFragment.activity;

import android.content.Intent;
import android.nfc.NdefRecord;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import common.BaseActivity;
import utils.KeyBoard;
import utils.ToastUtils;

/**
 * Created by Administrator on 2017/3/31.
 */

@ContentView(R.layout.modify_nick_name)
public class ModifyNickName extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.modify_nick_name_back)
    private TextView modify_nick_name_back;
    @ViewInject(R.id.modify_nick_name_save)
    private TextView modify_nick_name_save;
    @ViewInject(R.id.modify_nick_name_edt)
    private EditText modify_nick_name_edt;
    @ViewInject(R.id.modify_nick_name_delete)
    private ImageView modify_nick_name_delete;

    private String nickName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        nickName = getIntent().getStringExtra("nickName");
        initView();
        KeyBoard.showKeyBoard();
    }


    private void initView() {
        modify_nick_name_back.setOnClickListener(this);
        modify_nick_name_save.setOnClickListener(this);
        modify_nick_name_delete.setOnClickListener(this);
        modify_nick_name_edt.setText(nickName);
            if(nickName.length()>=8) {
                modify_nick_name_edt.setSelection(8);
            }else{
                modify_nick_name_edt.setSelection(nickName.length());
            }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.modify_nick_name_back:
                ModifyNickName.this.finish();
                break;
            case R.id.modify_nick_name_delete:
                nickName = "";
                modify_nick_name_edt.setText("");
                break;

            case R.id.modify_nick_name_save:
                if (isNameCorrect()) {
                    Intent i = new Intent();
                    i.putExtra("nickName", nickName);
                    setResult(RESULT_OK, i);
                    ModifyNickName.this.finish();
                }
                break;
            default:
                break;
        }
    }

    private boolean isNameCorrect() {
        if (modify_nick_name_edt.getText().toString().trim().length() < 2) {
            ToastUtils.shortToast("昵称太短");
        } else {
            nickName = modify_nick_name_edt.getText().toString().trim();
            return true;
        }
        return false;
    }

}
